$(function() {
	// 教师数据
	// layui表格
	layui.use(['table','layer',"form"], function() {
		var table = layui.table;
		var layer = layui.layer;
		var form = layui.form;
		var T =table.render({
			elem : '#TeacherTable',
			page : true,
			limit:10,
			limits:[5,10,15,20],
			url : '/teacherSet', // 数据接口

			response : {
				statusCode : 200
			// 重新规定成功的状态码为 200，table 组件默认为 0
			},
			parseData : function(res) { // res 即为原始返回的数据
				return {
					"count" : res.tol,
					"code" : res.status, // 解析接口状态
					"msg" : res.message, // 解析提示文本
					"data" : res.teacher
				// 解析数据列表
				};
			},
			cols : [ [ // 表头
			{
				field : 'tchId',
				title : '工号',
				width : 200,
				sort : true,
				fixed : 'left'
			}, {
				field : 'tchName',
				title : '教师名称'
			}, {
				field : 'courseName',
				title : '课程',
				
			}, {
				field : 'deptName',
				title : '院系',
				
			}, {
				field : 'operate',
				toolbar : '#barDemo',
				title : '操作',
				width : 220
			} ] ]
		});
		
		// 监听删除按钮

		table.on('tool(TeacherTable)', function(obj) {
			var data = obj.data;
	
			if (obj.event == 'del') {

				layer.confirm('真的删除行吗', function(index) {
					$.ajax({
						url : "/deleteTeacher",
						type : "post",
						data : {
							"tchId" : data.tchId
						},
						dataType : "json",
						success : function() {
							obj.del();
							layer.close(index);
							layer.msg("删除成功");
						},
						error : function() {
							layer.msg("error");
							layer.close(index);
						}
					})

				});
			}else if (obj.event == 'edit'){  		// 监听编辑按钮
				if(obj.event == 'edit'){
			      	   var data = obj.data;			      	   
						layer.open({
							title:'教师信息',
							type : 1,
							skin : 'layui-layer-rim', // 加上边框
							area : [ '520px',
									'400px' ], // 宽高
							content : '<form class="layui-form layui-form-pane" action="#" id="form1">\n'
									+ '  <br>\n'
									+ '  <div class="layui-form-item">\n'
									+ '    <label class="layui-form-label">工号</label>\n'
									+ '    <div class="layui-input-block" >\n'
									+ '      <input type="text" id="input1" name="tchId" lay-verify="title" autocomplete="off" readonly disabled class="layui-input form-control" >\n'
									+ '    </div>\n'
									+ '  </div>\n'
									+ '  <div class="layui-form-item">\n'
									+ '    <label class="layui-form-label">姓名</label>\n'
									+ '    <div class="layui-input-block">\n'
									+ '      <input type="text" id="input2" name="tchName" lay-verify="title" autocomplete="off"  disabled readonly class="layui-input form-control">\n'
									+ '    </div>\n'
									+ '  </div>\n'
									+ '  <div class="layui-form-item">\n'
									+ '    <label class="layui-form-label">院系</label>\n'
									+ '    <div class="layui-input-block">\n'
									+ '      <select id="select1" name="deptName" lay-filter="select1" lay-verify="">\n'
									+ '        <option></option>\n'
									+ '      </select>\n'
									+ '    </div>\n'
									+ '  </div>'
									+ '  <div class="layui-form-item">\n'
									+ '    <label class="layui-form-label">课程</label>\n'
									+ '    <div class="layui-input-block">\n'
									+ '      <select id="select2" name="courseName" lay-verify="">\n'
									+ '        <option></option>\n'
									+ '      </select>\n'
									+ '    </div>\n'
									+ '  </div>\n'
									+ '<div><button type="button" id="form1_btn" class="layui-btn" style="margin-left: 160px;">提交</button></div>\n'
									
									+ '</form>'
						});
                      var tchId = data.tchId;
                      var tchName = data.tchName;
								
						// 添加数据到表
						$("#input1").val(tchId);// 学生ID
						$("#input2").val(tchName);// 学生姓名
			// $("#from1 [name='deptName']>option:eq(0)").text(deptName);// 院系
			// $("#from1 [name='className']:eq(0)").text(className);// 班级

						$.ajax({
							url : '/teacher_dept',
							dataType : 'json',
							type : 'post',
							success : function(data) {
								var d = eval(data);
								console.log(d);
								for (var i = 0; i < d.length; i++) {
									$("#select1").append("<option value="+d[i].deptId+">"+d[i].deptName+"</option>")
									console.log("......"+d[i].deptName+".."+d[i].deptId);
									form.render('select');
								}
							}
						})
								
								$.ajax({
									url :'/Course',
									dataType : 'json',
									type : 'post',
									success : function(data) {
										var d = eval(data);
										console.log(d);
										$("#select2").empty();
										$("#select2").append("<option></option>");
										for (var i = 0; i < d.length; i++) {
											$("#select2").append("<option value="+d[i].courseId+">"+d[i].courseName+"</option>")
											console.log("......"+d[i].courseName+".."+d[i].courseId);
											form.render('select');
										}
									},error:function(){
										layer.alert('系统错误',{
											icon : 2,
											skin : 'layer-ext-moon'
										})
									}
								})
								
						// 修改提交
						$("body").on("click","#form1_btn",function(){
						var deptName = $("#select1 option:selected").text();
						var deptId = $("#select1 option:selected").val();
						var courseName = $("#select2 option:selected").text();
						var courseId = $("#select2 option:selected").val();
						var tchId = $("#input1").val();
						console.log(deptId,deptName);
						console.log(courseId,courseName);
									
						if(deptName !=null && courseName!= null && deptName != '' && courseName != ''){
							$.ajax({
							url:'/updateTeacher',
							dataType:'json',
							type:'post',
							data:{
								"tchId":tchId,
								"deptId":deptId,
                                "courseId":courseId								
							},
							success:function(r){
								if(r.msg =="YES"){
									layer.closeAll();
									T.reload();
								}
								layer.msg("信息修改成功");
							},
							error:function(){
								layer.alert('系统错误',{
									icon : 2,
									skin : 'layer-ext-moon'
								})
							}
						})
					}else{
						layer.alert('请将信息填写完整', {
							icon: 0,
							skin: 'layer-ext-moon' 
							})
					}
				})			
					


						}
						
				
			}else if(obj.event = "RP"){
				
				layer.confirm('确定重置密码？',function(index) {
					
					
					$.ajax({
						url:'/TeacherRP',
						dataType:'json',
						type:'post',
						data:{
							"tchId" : data.tchId
						},success:function(res){
							if(res.msg == 'YES'){
								layer.msg("密码重置成功");
							}
						},error:function(){
							layer.alert('系统错误',{
								icon : 2,
								skin : 'layer-ext-moon'
							})
						}
					})
				})
			}
			form.render();
		});



	// 院系选择按钮
	layui.use('layer', function() {
		var layer = layui.layer;
		$.ajax({
			url : '/teacher_dept',
			dataType : 'json',
			type : 'post',
			success : function(data) {
				var da = eval(data);
				console.log(da.length);

				for (var i = 0; i < da.length; i++) {
					$("#department_select1").append(
							"<button type=\"button\" class=\"btn btn-default\">"
									+ da[i].deptName + "</button>");
				}
			},
			error : function() {
				layer.alert('院系信息读取失败', {
					icon : 2,
					skin : 'layer-ext-moon'
				})
			}
		})

	})

	// 院系选择显示并查询

		$("#department_select1").on("click","button", function() {
			var $text = $(this).text();
			console.log($text);
			$("#span").html($text);

			layui.use('table', function() {
				var table = layui.table;
				table.reload('TeacherTable', {
					url : '/dept_teacher',
					where : {
						"deptName" : $text
					},
					page : {
						curr : 1
					// 重新从第 1 页开始
					}
				})

			})
		})


	// 模糊查询
	$("#btn_select1").on("click", function() {
		var $msg = $("#select_input1").val();
        var deptName = $("#span").text();

		layui.use('table', function() {
			var table = layui.table;
			table.reload('TeacherTable', {
				url : '/fuzzy_query',
				where : {
					"text" : $msg,
					"deptName" : deptName
				},
				page : {
					curr : 1
				// 重新从第 1 页开始
				}
			})

		})
	})
	// 回车键模糊查询
	        $("#select_input1").on('keydown', function (event) {
            if (event.keyCode == 13) {
        		var $msg = $("#select_input1").val();
                var deptName = $("#span").text();

        		layui.use('table', function() {
        			var table = layui.table;
        			table.reload('TeacherTable', {
        				url : '/fuzzy_query',
        				where : {
        					"text" : $msg,
        					"deptName" : deptName
        				},
        				page : {
        					curr : 1
        				// 重新从第 1 页开始
        				}
        			})

        		})
                
                return false
            }
        });


	// 添加教师
	$("#Add_Teacher")
			.on(
					"click",
					function() {
						
					    setTimeout(function() {
					    	$("#button1").on("click",function(){
					    		var tchId = $("input[name='tchId']").val();	
					    		var tchName = $("input[name='tchName']").val();
					    		var courseName = $("#select2 option:selected").text();
					    		var deptName = $("#select1 option:selected").text();
					    		if(tchId == "" || tchName == ""|| courseName == "" || deptName == "" ){
					    			layer.alert("数据不能为空");
					    		}else{
					    			
					    		
							    		$.ajax({
							    			type:"post",
							    			url:"/insertTeacher",
							    			datatype : 'json',
							    			data : {
							    				"tchId" : tchId,
							    				"tchName" : tchName,
							    				"courseName" : courseName,
							    				"deptName" : deptName
							    			},
							    			success : function(data) {
							                	 if(data.msg == "教师号被占有"){
							                		 layer.alert("教师号已有");		                					                		 
							                	 }
							                	 if(data.msg == "成功"){
							                		
							                		 layer.closeAll();
														T.reload();
														 layer.msg("添加成功");
							                	 }
							    			}
							    		});
					    		}
					    		
					    	})

					       }, 200);
						
						layer
								.open({
									type : 1,
									skin : 'layui-layer-rim', // 加上边框
									area : [ '520px', '400px' ], // 宽高
									content : '<form class="layui-form" action="#">\n'
											+ '<br>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">工号</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <input type="text" name="tchId" lay-verify="title" autocomplete="off" placeholder="工号不能相同" class="layui-input" >\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">姓名</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <input type="text" name="tchName" lay-verify="title" autocomplete="off" placeholder="不能为空" class="layui-input">\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">院系</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <select id="select1" name="deptName" lay-filter="select1" lay-verify="">\n'
											+ '        <option></option>\n'
											+ '      </select>\n'
											+ '    </div>\n'
											+ '  </div>'
											+ '  <div class="layui-form-item">\n'
											+ '    <label class="layui-form-label">课程</label>\n'
											+ '    <div class="layui-input-block">\n'
											+ '      <select id="select2" name="courseName" lay-verify="">\n'
											+ '        <option></option>\n'
											+ '      </select>\n'
											+ '    </div>\n'
											+ '  </div>\n'
											+ '<div id = ><button type="button" class="layui-btn" style="margin-left: 160px;" id="button1">提交</button>\n'
											+ '<button type="reset" class="layui-btn layui-btn-primary" id="button2">重置</button></div>\n'
											+ '</form>'
								});
						$.ajax({
							url : '/teacher_dept',
							dataType : 'json',
							type : 'post',
							success : function(data) {
								var d = eval(data);
								console.log(d);
								for (var i = 0; i < d.length; i++) {
									$("#select1").append("<option value="+d[i].deptId+">"+d[i].deptName+"</option>")
									console.log("......"+d[i].deptName+".."+d[i].deptId);
									form.render('select');
								}
							}
						})
								
								$.ajax({
									url :'/Course',
									dataType : 'json',
									type : 'post',
									success : function(data) {
										var d = eval(data);
										console.log(d);
										$("#select2").empty();
										$("#select2").append("<option></option>");
										for (var i = 0; i < d.length; i++) {
											$("#select2").append("<option value="+d[i].courseId+">"+d[i].courseName+"</option>")
											console.log("......"+d[i].courseName+".."+d[i].courseId);
											form.render('select');
										}
									},error:function(){
										layer.alert('系统错误',{
											icon : 2,
											skin : 'layer-ext-moon'
										})
									}
								})
						
						
					})
	});		
		
					

	
});